package com.app;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.app.common.util.ID;
import com.app.pojo.Demo;
import com.app.service.IDemoService;
import com.github.pagehelper.PageInfo;

//2020-10-11 15:53:45
@RequestMapping(value = IndexController.PATH, name = "直播API")
@RestController("LiveApiController")
public class IndexController {
	private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(IndexController.class);
	public static final String PATH = "index";

	// @Autowired
	// RabbitProducer rabbitProducer;
	@Autowired
	IDemoService demoService;

	@PostMapping(value = "/sendMessage")
	public Map<String, Object> sendMessage(@RequestBody Map<String, Object> ps) {
		System.out.println(ps);
		String msg = String.format("%s @%s", (String) ps.get("msg"), LocalDateTime.now());
		// rabbitProducer.convertAndSend(msg);

		// 保存一条数据
		Demo d = new Demo();
		d.setId(String.valueOf(System.currentTimeMillis()));
		d.setName(msg);
		demoService.save(d);

		return Map.of("success", true, "message", "成功");
	}

	// 分页查询
	@PostMapping(value = "/listPage", name = "分页查询列表")
	public PageInfo<Demo> listPage(@RequestBody Map<String, Object> pm) {
		return demoService.queryByPage(pm);
	}

	// 查询clickhouse
	@PostMapping(value = "/list")
	public Map<String, Object> list(@RequestBody Map<String, Object> ps) {
		List<Demo> list = demoService.queryForList(null);
		System.out.println("LIST-------" + System.currentTimeMillis());
		return Map.of("success", true, "message", "成功", "data", list);
	}

	// 删除
	@PostMapping(value = "/deleteByIds", name = "删除")
	public Integer deleteByIds(@RequestBody List<String> ids) {
		return demoService.deleteByIds(ids);
	}

	// 更新
	@PostMapping(value = "/update", name = "更新")
	public Integer update(@RequestBody Demo vo) {
		vo.setName(vo.getName() + " update");
		return demoService.update(vo);
	}

	// 更新
	@GetMapping(value = "/test")
	public Long test() {
		Demo d = new Demo();
		d.setId(ID.get());
		d.setName(LocalDateTime.now().toString());
		demoService.save(d);

		return System.currentTimeMillis();
	}

}